package com.symc.his.api.impl.doctor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.symc.his.api.base.BaseApiService;
import com.symc.his.api.base.BaseResponse;
import com.symc.his.api.doctor.MedicalOrderService;
import com.symc.his.api.doctor.dto.req.MedicalOrderSaveInspectReqDTO;
import com.symc.his.api.doctor.dto.resp.MedicalOrderRespDTO;
import com.symc.his.api.examination.dto.req.InspectTermReqDTO;
import com.symc.his.api.impl.entity.MedicalOrderDO;
import com.symc.his.api.impl.feign.ExaminationServiceFeign;
import com.symc.his.api.impl.mapper.MedicalOrderMapper;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @CreateTime: 2023/4/22 22:07
 * @Version:
 * @Since: 1.8
 * @Author: 辛凤文 forward.seen@foxmail.com
 */
@RestController
public class MedicalOrderServiceImpl extends BaseApiService implements MedicalOrderService {
    @Resource
    private MedicalOrderMapper medicalOrderMapper;

    @Resource
    private ExaminationServiceFeign examinationServiceFeign;


    @Override public BaseResponse<MedicalOrderRespDTO> getMedicalOrderListByVisitedRecordId(String visitedRecordId) {
        List<MedicalOrderDO> dos = medicalOrderMapper.selectList(new LambdaQueryWrapper<MedicalOrderDO>().eq(MedicalOrderDO::getVisitRecordId, visitedRecordId));
        List<MedicalOrderRespDTO> medicalOrderRespDTOList = dos.stream().map(e ->
            DO2DTO(e, MedicalOrderRespDTO.class)
        ).collect(Collectors.toList());
        return setResultSuccess(medicalOrderRespDTOList);
    }

    @Override
    public BaseResponse saveInspectMedicalOrder(String visitRecordId, MedicalOrderSaveInspectReqDTO inspectTermList) {
        //TODO 根据visitRecordId查询门诊就诊表的数据，存储并存入医嘱表
        //查看数据库是否存在visitRecordId，不存在则创建，获取数据，医嘱类别为4；存在则跳过
        MedicalOrderDO medicalOrder = medicalOrderMapper.selectOne(new LambdaQueryWrapper<MedicalOrderDO>().eq(MedicalOrderDO::getVisitRecordId, visitRecordId));
        if (medicalOrder == null) {
            medicalOrder = new MedicalOrderDO();
            medicalOrder.setVisitRecordId(visitRecordId);
            medicalOrder.setMedicalOrderTypeCode("4");
            medicalOrderMapper.insert(medicalOrder);
        }
        //3.从检查医嘱表中根据visitRecordId和医嘱类别返回检查code集合A
        BaseResponse<List<String>> inspectTermRes = examinationServiceFeign.getInspectTermCodeListByVisitRecordIdAndTypeCode(visitRecordId, "4");
        List<String> inspectCodeListA = inspectTermRes.getData();
        //4.获取用户传过来的检查code集合B
        List<String> inspectCodeListB = inspectTermList.getInspectMedicalOrderCodeList();
        //5.两个code集合做交集A中无B的部分增加，B中无A的部分删除，考虑用Stream
        inspectCodeListA.forEach(code -> {
            if (!inspectCodeListB.contains(code)) {
                InspectTermReqDTO deleteDTO = new InspectTermReqDTO();
                deleteDTO.setVisitRecordId(visitRecordId);
                deleteDTO.setInspectTermCode(code);
                examinationServiceFeign.deleteInspectTermByVisitRecordIdAndInspectTermCode(deleteDTO);
            }
        });
        inspectCodeListB.forEach(code -> {
            if (!inspectCodeListA.contains(code)) {
                InspectTermReqDTO insertDTO = new InspectTermReqDTO();
                insertDTO.setVisitRecordId(visitRecordId);
                insertDTO.setInspectTermCode(code);
                insertDTO.setMedicalOrderTypeCode("4");
                BaseResponse<String> itemFeeRes = examinationServiceFeign.getItemFeeNameByItemFeeCode(code);
                String inspectName = itemFeeRes.getData();
                insertDTO.setInspectTermName(inspectName);
                examinationServiceFeign.insertInspectTerm(insertDTO);
            }
        });
        //6.增加删除都为RPC
        return setResultSuccess();
    }
}